package com.nbicc.common.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: lilingyan
 * @Date 2018/11/19 17:04
 */
public class HtmlUtils {

    /**
     *
     * @param bodyString
     * @param length        截取长度
     * @return
     */
    public static String bodyTextParse(String bodyString , Integer length){
        String bodyText = bodyTextParse(bodyString);
        if(length == null || length > bodyText.length()){
            return bodyText;
        }
        return bodyText.substring(0 , length);
    }

    public static String bodyTextParse(String bodyString){
        bodyString = "<root>" + bodyString + "</root>";
//        StringBuffer sb = new StringBuffer();
//        Document document = Jsoup.parse(bodyString);
//        Elements ele = document.getAllElements();
//        for (Element element : ele) {
//            sb.append(element.text().trim());
//        }
//        return sb.toString();
        return Jsoup.parse(bodyString).text();
    }

    public static Set<String> imgSrcParse(String imgStr){
        Set<String> imgIds = new HashSet<>();
        if(StringUtils.isEmpty(imgStr)){
            return imgIds;
        }
        imgStr = "<root>" + imgStr + "</root>";
        Document document = Jsoup.parse(imgStr);
        Elements elements = document.select("img[src]");
        for (Element element : elements) {
            imgIds.add(element.attr("src").trim());
        }
        return imgIds;
    }

}
